﻿using System;
using System.Collections.Generic;
using System.Text;
using ReaXML;
using System.Windows.Forms;
using System.IO;

namespace SCH_Tools.Modules
{
    class ManagmentCmd
    {
        public void ExecuteSqlCmd(string file, Server server, ServerDriver Driver, string Path_Output)
        {
            string strExecutecmd = string.Empty;
            /*
            strExecutecmd = string.Format("-{0} \"{1}\" -{2} \"{3}\" -{4} {5} -{6} {7} -{8} \"{9}\" ",
                Driver.ParamHost, server.ServerName,
                Driver.ParamServer, server.Ip,
                Driver.ParamUser, server.User,
                Driver.ParamPass, server.Pass,
                Driver.ParamInput, file);*/

            //HOST
            if (!string.IsNullOrEmpty(server.Host))
            {
                strExecutecmd = string.Format("-{0} \"{1}\" ", Driver.ParamHost, server.Host);
            }

            //SERVER-ALIAS
            if (!string.IsNullOrEmpty(server.Ip) || !string.IsNullOrEmpty(server.Alias))
            {
                strExecutecmd = string.Concat(strExecutecmd, string.Format("-{0} \"{1}\" ", Driver.ParamServer, (string.IsNullOrEmpty(server.Ip) ? server.Alias : string.Concat(server.Ip, (string.IsNullOrEmpty(server.Port) ? string.Empty : string.Concat(",", server.Port))))));
            }

            //AUTENTICATION
            strExecutecmd = string.Concat(strExecutecmd, string.Format("-{0} {1} -{2} {3} -{4} \"{5}\" ",
                Driver.ParamUser, server.User,
                Driver.ParamPass, server.Pass,
                Driver.ParamInput, file));

            //OUTPUT
            if (!string.IsNullOrEmpty(Driver.ParamOutput))
            {
                strExecutecmd = string.Concat(strExecutecmd, string.Format("-{0} \"{1}\" ", Driver.ParamOutput, Path.Combine(Path_Output, string.Concat(Path.GetFileName(file), ".out"))));
            }

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;

            if (string.IsNullOrEmpty(Driver.Path))
            {
                startInfo.FileName = Driver.CmdName;
            }
            else
            {
                startInfo.FileName = Driver.Path;
            }

            startInfo.Arguments = strExecutecmd;
            process.StartInfo = startInfo;
            process.Start();
        }
    }
}
